﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    public class StudentBS
    {
        static public Student GetStudent(string student_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Student model = new Student();
                var student = db.student.FirstOrDefault(x => x.student_id == student_id);
                model.student_id = student.student_id;
                model.student_name = student.student_name;
                model.student_sex = student.student_sex;
                model.pro_emphasis_id = student.pro_emphasis_id;
                model.pro_emphasis_name = student.pro_emphasis.pro_emphasis_name;
                return model;
            }
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        static public bool AddStudent(Student student)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.student.AddObject(new student()
                    {
                        student_id = student.student_id,
                        student_name = student.student_name,
                        student_sex = student.student_sex,
                        class_id = student.class_id,
                        pro_emphasis_id = student.pro_emphasis_id
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 学生选课
        /// </summary>
        /// <returns>成功与否</returns>
        static public bool AddCourse(string student_id, string course_id)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    //var ret = db.teacher.Where(x => x.teacher_id == teacher_id);
                    var student = db.student.FirstOrDefault(x => x.student_id == student_id);
                    if (student.course == null) //外键表，要自己new不？
                    {
                        student.course = new System.Data.Objects.DataClasses.EntityCollection<course>();
                    }
                    var target = db.course.FirstOrDefault(c => c.course_id == course_id);
                    student.course.Add(target);
                    db.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
